package com.lemon.components.ant;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;

import com.alibaba.fastjson.JSONArray;
import com.lemon.components.ant.beans.TableColumns;
import com.lemon.components.ant.beans.TemplateContext;
import com.lemon.components.ant.util.ClassUtil;
import com.lemon.components.ant.util.CodeMakerPropertiesUtil;
import com.lemon.components.ant.util.TemplateUtil;

public class ControllerTemplateAntTask extends Task {
	
	private String projectdir;
	private String serviceName;
	private String modelName;
	private String servicePackage;
	private String controllerPackages;
	private String controllerPrefix;
	
	@Override
	public void execute() throws BuildException {
		String templatepath = projectdir+"\\src\\main\\resources\\code\\codeTemplate";
		String outputfilepath = projectdir+"\\src\\main\\java\\"+controllerPackages;
		log("模版目录："+templatepath,Project.MSG_WARN);
		log("输出："+outputfilepath,Project.MSG_WARN);
		HashMap<String, Object> params = new HashMap<>();
		
		List<TableColumns> columnList = JSONArray.parseArray(CodeMakerPropertiesUtil.get("columns"), TableColumns.class);
		
		TemplateContext context = new TemplateContext();
		context.setServicePackage(ClassUtil.filepath2classpath(servicePackage));
		context.setControllerPackage(ClassUtil.filepath2classpath(controllerPackages));
		context.setServiceName(serviceName);
		context.setModelName(modelName);
		context.setControllerPrefix(controllerPrefix);
		context.setColumns(columnList);
		params.put("info", context);
		
		columnList.forEach(item -> {
			if(item.getSelect()!=null){
				String[] tmpClazz = item.getSelect().getClazz().split("\\.");
				if(tmpClazz.length > 1){
					item.getSelect().setRealClassPath(item.getSelect().getClazz());
				} else {
					item.getSelect().setRealClassPath(context.getServicePackage()+".enums."+item.getSelect().getClazz());
				}
				
			}
		});
		
		//Controller
		TemplateUtil.getInstance(templatepath).build(
				"controller/TemplateController.java",
				outputfilepath+"\\"+StringUtils.capitalize(modelName)+"Controller.java",
				params
			);
		log("Controller访问地址："+controllerPrefix+"/"+serviceName+"/topage.htm",Project.MSG_WARN);
	}

	public String getProjectdir() {
		return projectdir;
	}

	public void setProjectdir(String projectdir) {
		this.projectdir = projectdir;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getControllerPrefix() {
		return controllerPrefix;
	}

	public void setControllerPrefix(String controllerPrefix) {
		this.controllerPrefix = controllerPrefix;
	}

	public String getServicePackage() {
		return servicePackage;
	}

	public void setServicePackage(String servicePackage) {
		this.servicePackage = servicePackage;
	}

	public String getControllerPackages() {
		return controllerPackages;
	}

	public void setControllerPackages(String controllerPackages) {
		this.controllerPackages = controllerPackages;
	}

	public String getModelName() {
		return modelName;
	}

	public void setModelName(String modelName) {
		this.modelName = modelName;
	}

}
